import config from "./config";

const getToken = () => {
  return "";
};

const requset = (url, data = {}, method) => {
  return new Promise((resolve, reject) => {
    uni.requset({
      url: config.baseUrl + url,
      headers: {
        "Content-Type": "application/json",
        Authorization: getToken()
      },
      data,
      method,
      success: res => {
        if (res.data) {
          resolve(res.data);
        }
      },
      fail: err => reject(err)
    });
  });
};

const get = (url, data) => {
  return requset(url, data, "GET");
};
const post = (url, data) => {
  return requset(url, data, "POST");
};

const upload = (path, form) => {
  return new Promise((resolve, reject) => {
    let url = config.baseUrl + "/minio/fileUpload";
    uni.uploadFile({
      url: url,
      header: {},
      filePath: path,
      name: "file",
      formData: form,
      success: res => {
        let data = JSON.parse(res.data);
        resolve(data);
      },
      fail: error => {
        reject(error);
      }
    });
  });
};

export { get, post, upload };
